package ltd.dujiabao.cache_processor;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

/**
 * @author : DuJiabao
 * @Project : cache_processor
 * @Time : 2022/1/15 11:23
 * @Desc :
 */
public class PathUtils {
    public static Optional<String> readFile(Path path) {
        try {
            return Optional.of(new String(Files.readAllBytes(path)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }

    public static Optional<Path> findFirstPathEndWiths(Path path, String endWiths) {
        try {
            return Files.list(path).filter(p -> p.toString().endsWith(endWiths)).findFirst();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Optional.empty();
    }

    public static String replaceAllIllegalChar(String str) {
        return str.replaceAll("[\\/:*?\"<>|]", "&");
    }

}
